﻿using System;
using System.Linq;
using JetBrains.ReSharper.Daemon;
using JetBrains.ReSharper.Daemon.Stages;
using JetBrains.ReSharper.Daemon.Xml.Highlightings;
using JetBrains.ReSharper.Psi.Tree;
using JetBrains.ReSharper.Psi.Xml;
using JetBrains.ReSharper.Psi.Xml.Tree;
using SPCAFContrib.ReSharper.Common;
using SPCAFContrib.ReSharper.Common.Attributes;
using SPCAFContrib.ReSharper.Common.Extensions;
using SPCAFContrib.ReSharper.Common.Psi.XmlCache;
using SPCAFContrib.ReSharper.Common.XmlAnalysis;
using SPCAFContrib.ReSharper.Consts;
using SPCAFContrib.ReSharper.Inspection.Xml.SPCAF;

[assembly: RegisterConfigurableSeverity(SPC010703Highlighting.CheckId,
  null,
  Consts.SPCAF_CORRECTNESS_GROUP,
  SPC010703Highlighting.CheckId + ": " + SPC010703Highlighting.Message,
  "The attribute 'VisibilityFeatureDependency' is not supported for Features with Web application scope.",
  Severity.ERROR,
  false, Internal = false)]

namespace SPCAFContrib.ReSharper.Inspection.Xml.SPCAF
{
    [Applicability(
        IDEProjectType.SP2010FarmSolution |
        IDEProjectType.SPSandbox |
        IDEProjectType.SP2013FarmSolution)]
    public class DoNotAddDependencyToWebAppFeatureInSiteDefinition : XmlTagProblemAnalyzer
    {
        public override void Run(IXmlTag element, IHighlightingConsumer consumer)
        {
            if (element.GetProject().IsApplicableFor(this))
            {
                if (IsInvalid(element))
                {
                    SPC010703Highlighting errorHighlighting = new SPC010703Highlighting(element);
                    consumer.ConsumeHighlighting(element.Header.GetDocumentRange(), errorHighlighting);
                }
            }
        }

        public static bool IsInvalid(IXmlTag element)
        {
            bool result = false;

            if (element.Header.ContainerName == "Configuration")
            {
                if (element.AttributeExists("VisibilityFeatureDependency"))
                {
                    IXmlAttribute attribute = element.GetAttribute("VisibilityFeatureDependency");
                    var project = element.GetProject();
                    Guid featureId = Guid.Empty;
                    if (Guid.TryParse(attribute.UnquotedValue, out featureId))
                    {
                        result =
                            FeatureCache.GetInstance(project.GetSolution())
                                .Items.Any(
                                    feature =>
                                        feature.Id.Equals(featureId) && feature.Scope == SPFeatureScope.WebApplication);
                    }
                }
            }

            return result;
        }
    }

    [ConfigurableSeverityHighlighting(CheckId, XmlLanguage.Name, OverlapResolve = OverlapResolveKind.NONE, ShowToolTipInStatusBar = true)]
    public class SPC010703Highlighting : XmlErrorHighlighting
    {
        public const string CheckId = "SPC010703";
        public const string Message = "Do not reference WebApplication Feature in attribute 'VisibilityFeatureDependency'";

        public IXmlTag Element { get; private set; }

        public SPC010703Highlighting(IXmlTag element) :
            base(String.Format("{0}: {1}", CheckId, Message))
        {
            Element = element;
        }
    }
}
